package com.sushi.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import lombok.extern.slf4j.Slf4j;

/**
 * IO 工具
 * @author Administrator
 */
@Slf4j
public class IOUtil {

    public static boolean writeSomething(String targetDir, String fileName, String fileType, String content) {
        File file = new File(targetDir + "/"  + fileName.concat(".").concat(fileType));
        log.info("写入路径：{}", targetDir);
        log.info("写入名称：{}", fileName);
        log.info("写入类型：{}", fileType);
        log.info("写入路径：{}", file.getPath());
        if (!file.getParentFile().exists()) {
            log.info("创建了一个文件夹，路径为：{}", file.getParentFile().getAbsolutePath());
            file.getParentFile().mkdirs();
        }
        try {
            boolean newFile = file.createNewFile();
            if (!newFile) { return false; }
            log.info("创建了一个文件，路径为：{}", file.getAbsolutePath());
            FileOutputStream outputStream = new FileOutputStream(file);
            byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
            outputStream.write(bytes);
            outputStream.close();
        }
        catch (FileNotFoundException e) {
            log.info("文件未找到");
            return false;
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

}
